package cemp.controller;

import cemp.common.dto.Response;
import cemp.common.dto.request.StockLoginRequestDto;
import cemp.common.dto.request.StockSysUserRequestDto;
import cemp.dto.ro.LoginResponse;
import cemp.service.StockSysUserService;
import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;

@RestController
@AllArgsConstructor
@RequestMapping("/auth/shiro")
public class ShiroAuthController {

    @Autowired
    StockSysUserService stockSysUserService;

    @GetMapping("/print")
    public String list() {
        System.out.println("print");
        return "print";
    }

    //注册
    @PostMapping("/register")
    public void register(@RequestBody StockSysUserRequestDto sysuser) throws InvocationTargetException, IllegalAccessException {
        stockSysUserService.register(sysuser);
    }


    //登录
    @PostMapping("/login")
    public Response<LoginResponse> login(@RequestBody StockLoginRequestDto login) throws Exception {
        LoginResponse response = stockSysUserService.login(login);
        if(response != null){
            return Response.success(response);
        }else{
            return Response.error("500","登录失败");
        }
    }

    /**
     * 手机号一登录
     * @param login
     * @return
     * @throws Exception
     */
    @PostMapping("/phone/login")
    public Response<LoginResponse> phoneLogin(@RequestBody StockLoginRequestDto login) throws Exception {
        LoginResponse response = stockSysUserService.phoneLogin(login);
        return Response.success(response);
    }
}
